# Reproduction files - Sentiment

# Deepening Bridging and Moving Minds in Stressful Times







# Session Information -----------------------------------------------------




#R version 4.3.2 (2023-10-31 ucrt)
#Platform: x86_64-w64-mingw32/x64 (64-bit)
#Running under: Windows 10 x64 (build 19045)
#
#Matrix products: default
#
#locale:
#  [1] LC_COLLATE=German_Germany.utf8  LC_CTYPE=German_Germany.utf8    LC_MONETARY=German_Germany.utf8 LC_NUMERIC=C                   
#[5] LC_TIME=German_Germany.utf8    
#
#attached base packages:
#  [1] stats     graphics  grDevices utils     datasets  methods   base     
#
#other attached packages:
#  [1] haven_2.5.3     stargazer_5.2.3 sjmisc_2.8.9    quanteda_4.1.0  forcats_1.0.0   stringr_1.5.0   dplyr_1.1.4     purrr_1.0.1    
#[9] readr_2.1.4     tidyr_1.3.0     tibble_3.2.1    ggplot2_3.5.1   tidyverse_1.3.2
#
#loaded via a namespace (and not attached):
#  [1] httr_1.4.4          jsonlite_1.8.4      carData_3.0-5       modelr_0.1.10       assertthat_0.2.1    expm_0.999-7        gld_2.6.6          
#[8] lmom_2.9            googlesheets4_1.0.1 cellranger_1.1.0    pillar_1.10.1       backports_1.4.1     lattice_0.20-45     glue_1.6.2         
#[15] ggsignif_0.6.4      rvest_1.0.3         colorspace_2.1-0    Matrix_1.5-1        psych_2.2.9         pkgconfig_2.0.3     broom_1.0.3        
#[22] mvtnorm_1.1-3       scales_1.3.0        rootSolve_1.8.2.3   tzdb_0.3.0          timechange_0.2.0    proxy_0.4-27        googledrive_2.0.0  
#[29] generics_0.1.3      car_3.1-1           sjlabelled_1.2.0    ellipsis_0.3.2      ggpubr_0.6.0        withr_2.5.0         cli_3.6.0          
#[36] mnormt_2.1.1        magrittr_2.0.3      crayon_1.5.2        readxl_1.4.2        stopwords_2.3       fs_1.6.1            nlme_3.1-160       
#[43] MASS_7.3-58.1       rstatix_0.7.2       xml2_1.3.3          class_7.3-20        tools_4.2.2         data.table_1.14.6   hms_1.1.2          
#[50] gargle_1.3.0        lifecycle_1.0.3     Exact_3.2           munsell_0.5.0       reprex_2.0.2        compiler_4.2.2      e1071_1.7-13       
#[57] rlang_1.1.4         grid_4.2.2          rstudioapi_0.14     boot_1.3-28         DescTools_0.99.50   gtable_0.3.1        abind_1.4-5        
#[64] DBI_1.1.3           R6_2.5.1            lubridate_1.9.2     fastmatch_1.1-3     insight_0.19.0      stringi_1.7.12      parallel_4.2.2     
#[71] Rcpp_1.0.10         vctrs_0.6.5         dbplyr_2.3.0        tidyselect_1.2.0   


# Calculations ------------------------------------------------------------


# load necessary packages
library(tidyverse)
library(quanteda)
library(sjmisc)
library(stargazer)
library(haven)


# Data preparation --------------------------------------------------------

# read datasets
data_DE <- readRDS("data_de_wght.RDS")
data_AT <- readRDS("data_at_wght.RDS")

# Rauh 2018 Sentiment dictionary
load("dic_rauh.RData")


# filter datasets, keep only respondents with communicative intervention

data_DE_treatment <- data_DE %>% 
  filter(group_treatment <=3)

data_AT_treatment <- data_AT %>%
  filter(group_treatment <=3)


# calculate number of negative words in responses

# study 1 (germany)

data_DE_treatment[is.na(data_DE_treatment$treatment_response),]$nwords_response <- 0

sentiment_DE <- data_DE_treatment$treatment_response %>% 
  tokens() %>% 
  dfm() %>% 
  dfm_lookup(., dictionary_rauh)

sentiment_DE_df <- convert(sentiment_DE, to = "data.frame")

data_DE_treatment$negative_words <- sentiment_DE_df$negative

frq(data_DE_treatment$negative_words)



# study 2 (austria)

sentiment_AT <- data_AT_treatment$treatment_response %>% 
  tokens() %>% 
  dfm() %>% 
  dfm_lookup(., dictionary_rauh)

sentiment_AT_df <- convert(sentiment_AT, to = "data.frame")

data_AT_treatment$negative_words <- sentiment_AT_df$negative

frq(data_AT_treatment$negative_words)







# Regression models -------------------------------------------------------


# recode treatment germany
data_DE_treatment$treatment_fac <- car::recode(data_DE_treatment$group_treatment, recodes = "1='Contestatory'; 2='Collaborative'; 3='Open Communication'", as.factor = T)
data_DE_treatment$treatment_fac <- relevel(data_DE_treatment$treatment_fac, ref = "Open Communication")
frq(data_DE_treatment$treatment_fac)


# recode treatment austria
data_AT_treatment$treatment_fac <- car::recode(data_AT_treatment$group_treatment, recodes = "1='Contestatory'; 2='Collaborative'; 3='Open Communication'", as.factor = T)
data_AT_treatment$treatment_fac <- relevel(data_AT_treatment$treatment_fac, ref = "Open Communication")
frq(data_AT_treatment$treatment_fac)







m_DE <- lm(negative_words ~ treatment_fac + nwords_response, data = data_DE_treatment)
m_AT <- lm(negative_words ~ treatment_fac + nwords_response, data = data_AT_treatment)


stargazer::stargazer(m_DE, m_AT, type = "text", star.cutoffs = c(0.05, 0.01, 0.001))
